//活动
exports.comprehensive_activity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var skip  =  restfulParams.skip == undefined  ? 0 : restfulParams.skip;
    var limit =  restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    //任务流创建
    async.waterfall([

        //查询机构活动
            function (cb) {

                //console.log(CORE_APIConfig.coreAPI.search_organization_activity_info + "/sort_order/oa_browse_count-desc" + "/oa_type/5/skip/"+skip+"/limit/" + limit)

                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.search_organization_activity_info + "/sort_order/oa_browse_count-desc" + "/oa_type/5/oa_is_end/0/skip/"+skip+"/limit/" + limit,
                        method : 'POST',
                        data : {
                            keywords : "oa_category_text-" + restfulParams.keywords
                        }
                    },
                    function (err, data) {
    
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
    
                        resultData.organizationActivityData = data.data;
    
                        cb();
                    }
                );
    
    
            },

            //计算和自己的距离
        function(cb){

            for(var i = 0;i<resultData.organizationActivityData.length;i++){

                var tempLng = resultData.organizationActivityData[i].o_longitude;
                var tempLat = resultData.organizationActivityData[i].o_latitude;
                var distance = (common.GetDistance(tempLat,tempLng,restfulParams.lat,restfulParams.lng)).toFixed(1);    
                resultData.organizationActivityData[i].distance = distance;

            }

            cb();

        },

       
        //查询活动的类型和扩展
        function(cb){

            var tasks = [];

            //循环节点列表
            resultData.organizationActivityData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //拼团
                    if(item.oa_type == 0){
                       
                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.search_bulk_purchase_activity_config + "/bpac_oaid/" + item.oa_id,
                                method : 'POST',
                                data : {
                                    fields:["bpac_id","bpac_open_price"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log("拼团");
                                //console.log(data)
                                
                                if(data.data.length >0){
                                    item.price =  data.data[0].bpac_open_price;
                                }
                               

                                cb();
                            }
                        );
                        //投票
                    }else if(item.oa_type == 1){
                        //console.log("投票");
                        cb();



                        //砍价
                    }else if(item.oa_type == 2){
                        cb();
                        //海报
                    }else if(item.oa_type == 3){
                        //console.log("海报");
                        cb();
                    }else if(item.oa_type == 4){
                        cb();
                    }else if(item.oa_type == 5){
                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.search_discounts_activity_config + "/dac_oaid/" + item.oa_id,
                                method : 'POST',
                                data : {
                                    fields:["dac_id","dac_new_price"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                if(data.data.length > 0){
                                    item.price =  data.data[0].dac_new_price;
                                }
                                

                                cb();
                            }
                        );
                    }else{
                        cb();
                    }

                });

            });

            async.waterfall(tasks, cb);

        }

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}